package com.cw.wx.controller;

import com.cw.wx.handler.WxChatMsgFactory;
import com.cw.wx.handler.WxChatMsgHandler;
import com.cw.wx.utils.MessageUtil;
import com.cw.wx.utils.SHA1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * Description:
 *
 * @Author:
 * @Date: 2024/5/8 18:42
 */
@RestController
@Slf4j
public class CallBackController {
    private static final String token = "adwidhaidwoaid";
    @Resource
    private WxChatMsgFactory wxChatMsgFactory;

    /**
     * @author: 江传文
     * @date: 2024/5/8 20:40
     * 回调消息校验
     */
    // @RequestParamz注解将HTTP请求中的参数绑定到方法的参数上
    @GetMapping("/callback")
    public String callback(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr) {
        log.info("get验签请求参数: signature:{},timestamp:{},nonce:{},echostr:{}", signature, timestamp, nonce, echostr);
        String shaStr = SHA1.getSHA1(token, timestamp, nonce, "");
        if (signature.equals(shaStr)) {
            return echostr;
        }
        return "unknown";
    }

    // wx传的数据格式均为xml格式,此处需要指定
    @PostMapping(value = "/callback", produces = "application/xml;charset=UTF-8")
    public String callback(
            @RequestBody String requestBody,
            @RequestParam("signature") String signature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam(value = "msg_signature", required = false) String msgSignature) {
        log.info("接收到微信消息: requestBody:{}", requestBody);
        Map<String,String> messsageMap = MessageUtil.parseXml(requestBody);
        // ToUserName:开发者微信号
        // FromUserName:发送方账号（一个OpenID）
        String msgType = messsageMap.get("MsgType");
        String event = messsageMap.get("Event") == null ? "" : messsageMap.get("Event");
        log.info("msgType:{},event:{}",msgType,event);

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(msgType);
        // event不为空则拼接
        if (!StringUtils.isEmpty(event)){
            stringBuilder.append(".");
            stringBuilder.append(event);
        }
        String msgTypeKey = stringBuilder.toString();
        WxChatMsgHandler wxChatMsgHandler = wxChatMsgFactory.getHandlerByMsgType(msgTypeKey);
        if (Objects.isNull(wxChatMsgHandler)) {
            return "unknown";
        }
        String replyContent = wxChatMsgHandler.dealMsg(messsageMap);
        log.info("replyContent:{}",replyContent);
        return replyContent;
    }

}
// ToUserName:接收方账号
// FromUserName:开发者微信号
//    String content = "<xml>\n" +
//            "  <ToUserName><![CDATA["+fromUserName+"]]></ToUserName>\n" +
//            "  <FromUserName><![CDATA["+toUserName+"]]></FromUserName>\n" +
//            "  <CreateTime>12345678</CreateTime>\n" +
//            "  <MsgType><![CDATA[text]]></MsgType>\n" +
//            "  <Content><![CDATA[你好,我吃柠檬]]></Content>\n" +
//            "</xml>";